package com.movie.service.impl;

import com.movie.entity.Favorite;
import com.movie.mapper.FavoriteMapper;
import com.movie.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    public void addFavorite(Integer userId, Integer movieId) {
        // 检查是否已收藏
        if (!isFavorite(userId, movieId)) {
            Favorite favorite = new Favorite();
            favorite.setUserId(userId);
            favorite.setMovieId(movieId);
            favorite.setCreateTime(new Date());
            favoriteMapper.insertFavorite(favorite);
        }
    }

    @Override
    public void removeFavorite(Integer userId, Integer movieId) {
        favoriteMapper.deleteFavorite(userId, movieId);
    }

    @Override
    public boolean isFavorite(Integer userId, Integer movieId) {
        return favoriteMapper.checkIfFavorite(userId, movieId) > 0;
    }
}
